/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary_package;

import Entitys.Fabricante;
import Entitys.Pedido;
import Entitys.ProductoGeneral;
import Helpers.RegexFormatter;
import Helpers.RegularExpression;
import control_package.AutoCompleteControl;
import control_package.BussinesControl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.BadLocationException;
import services.FabricanteServices;
import services.PedidoServices;





/**
 *
 * @author MAURO
 */
public class OrderGui extends javax.swing.JPanel {
    private BussinesControl ped=new BussinesControl();
    private Pedido pedido=new Pedido();
    private PedidoServices peser=new PedidoServices();
    private FabricanteServices facServ=new FabricanteServices();
    
    private List<Order> orders=new ArrayList<Order>();
    private OrderTableModel tablePed =new OrderTableModel();
    private DefaultComboBoxModel PedidoComboboxModel=new DefaultComboBoxModel();
    private AutoCompleteControl aCControl=new AutoCompleteControl();
    

    /**
     * Creates new form OrderGui
     */
    public OrderGui() {
        initComponents();
        PedidoComboBoxModelFill();
        this.TablePed.setModel(tablePed);
        this.addOrderPanel.setVisible(false);
        TablePed.setRowHeight(30);
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    
    
    
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addOrderPanel = new javax.swing.JPanel();
        IdProJFTF = new javax.swing.JFormattedTextField();
        NameProFTF = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        PedidojComboBox = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        QuantFTF = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablePed = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "PEDIDOS", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Verdana", 0, 18))); // NOI18N
        setAutoscrolls(true);
        setPreferredSize(new java.awt.Dimension(1240, 640));

        IdProJFTF.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        IdProJFTF.setPreferredSize(new java.awt.Dimension(6, 40));
        IdProJFTF.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                IdProJFTFMouseMoved(evt);
            }
        });
        IdProJFTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IdProJFTFActionPerformed(evt);
            }
        });

        NameProFTF.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        NameProFTF.setPreferredSize(new java.awt.Dimension(1042, 40));
        NameProFTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NameProFTFActionPerformed(evt);
            }
        });
        NameProFTF.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                NameProFTFFocusLost(evt);
            }
        });
        NameProFTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                NameProFTFKeyReleased(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel2.setText("Proovedor:");

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel1.setText("Id:");

        PedidojComboBox.setFont(new java.awt.Font("Verdana", 0, 22)); // NOI18N
        PedidojComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        PedidojComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                PedidojComboBoxItemStateChanged(evt);
            }
        });
        PedidojComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PedidojComboBoxActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/db_add.png"))); // NOI18N
        jButton2.setText("<html><p>Añadir</p><p>Pedido</p></html>");
        jButton2.setPreferredSize(new java.awt.Dimension(200, 80));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel3.setText("Nombre Producto:");

        QuantFTF.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        QuantFTF.setPreferredSize(new java.awt.Dimension(6, 40));
        QuantFTF.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                QuantFTFMouseMoved(evt);
            }
        });
        QuantFTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                QuantFTFActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel4.setText("Cant:");

        javax.swing.GroupLayout addOrderPanelLayout = new javax.swing.GroupLayout(addOrderPanel);
        addOrderPanel.setLayout(addOrderPanelLayout);
        addOrderPanelLayout.setHorizontalGroup(
            addOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addOrderPanelLayout.createSequentialGroup()
                .addGroup(addOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(IdProJFTF, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                .addGroup(addOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(addOrderPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(NameProFTF, javax.swing.GroupLayout.PREFERRED_SIZE, 948, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(addOrderPanelLayout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(jLabel3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(addOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(QuantFTF, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 20, Short.MAX_VALUE))
            .addGroup(addOrderPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PedidojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        addOrderPanelLayout.setVerticalGroup(
            addOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addOrderPanelLayout.createSequentialGroup()
                .addGroup(addOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(IdProJFTF, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NameProFTF, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(QuantFTF, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(addOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(PedidojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        jButton1.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/Copia de icon_check.png"))); // NOI18N
        jButton1.setText("TERMINADO");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        TablePed.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        TablePed.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(TablePed);

        jButton3.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/db_add.png"))); // NOI18N
        jButton3.setText("<html><p>Añadir</p><p>Pedido</p></html>");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 854, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(addOrderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addOrderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(88, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void PedidojComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PedidojComboBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PedidojComboBoxActionPerformed

    private void PedidojComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_PedidojComboBoxItemStateChanged
//        String cadena="";
//        
//         
//             pedido= ped.MakeQuery(this.PedidojComboBox.getSelectedItem());   
//       
//        
//        
//        
//   
    
        // TODO add your handling code here:
    }//GEN-LAST:event_PedidojComboBoxItemStateChanged

    private void IdProJFTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IdProJFTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_IdProJFTFActionPerformed

    private void NameProFTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NameProFTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_NameProFTFActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        jButton3.setVisible(false);
        addOrderPanel.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void IdProJFTFMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_IdProJFTFMouseMoved
         if (!this.IdProJFTF.getText().isEmpty()){
            String s=ped.SearchNameById(Long.parseLong(this.IdProJFTF.getText()));
            
            if(NameProFTF.getText().isEmpty()){
                this.NameProFTF.setText(s);
            }else if(!NameProFTF.getText().equals(s)){
                this.NameProFTF.setText(s);    
            }
        }
    }//GEN-LAST:event_IdProJFTFMouseMoved

    private void NameProFTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NameProFTFKeyReleased
          
        if(evt.getKeyCode()!=8 && evt.getKeyCode()!= 127){
             if(!NameProFTF.getText().isEmpty())
                try {
                    aCControl.AddTextToTextField(NameProFTF,NameProFTF.getText(),false);
                } catch (BadLocationException ex) {
                    Logger.getLogger(SellGui.class.getName()).log(Level.SEVERE, null, ex);
             }
        }  
    }//GEN-LAST:event_NameProFTFKeyReleased

    private void NameProFTFFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_NameProFTFFocusLost
        Object Id=ped.SearchIdByName(NameProFTF.getText());        
        if(!Id.equals(-1)){
           IdProJFTF.setText(""+Id);
        }else{
            JOptionPane.showMessageDialog(null, "Ops.. no se encuentra producto", "advertencia", JOptionPane.OK_OPTION);
        }
    }//GEN-LAST:event_NameProFTFFocusLost

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
        if(IdProJFTF.getText().equals("")||NameProFTF.getText().equals("")||QuantFTF.getText().equals("")){
            JOptionPane.showMessageDialog(null, "campos vacíos","Advertencia", JOptionPane.OK_OPTION, new ImageIcon(getClass().getResource("/Icons/triste.png")));
        }else{
        Long id=Long.parseLong(this.IdProJFTF.getText());
        Order order=new Order(id,this.NameProFTF.getText(),Integer.parseInt(QuantFTF.getText()),this.PedidojComboBox.getSelectedItem().toString(), new Date());
        tablePed.add(order);
        TablePed.updateUI();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void QuantFTFMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_QuantFTFMouseMoved
        // TODO add your handling code here:
    }//GEN-LAST:event_QuantFTFMouseMoved

    private void QuantFTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QuantFTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_QuantFTFActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.removeAll();
    }//GEN-LAST:event_jButton1ActionPerformed

    public void PedidoComboBoxModelFill(){
           this.PedidoComboboxModel.addElement("Seleccione");
           for(Fabricante fac:facServ.readAll()){
           this.PedidoComboboxModel.addElement(fac.getNombreFabricante());
           }
     PedidojComboBox.setModel(PedidoComboboxModel);
 }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField IdProJFTF;
    private javax.swing.JFormattedTextField NameProFTF;
    private javax.swing.JComboBox PedidojComboBox;
    private javax.swing.JFormattedTextField QuantFTF;
    private javax.swing.JTable TablePed;
    private javax.swing.JPanel addOrderPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables


private void Integrity() {
    new RegexFormatter(RegularExpression.NATURAL_NUMBERS).install(QuantFTF);
    new RegexFormatter(RegularExpression.NATURAL_NUMBERS).install(IdProJFTF);//        new RegexFormatter(RegularExpression.WORDS).install(NameProFTF);
    }


}
class Order{
    private Long idPro;
    private String NombreP;
    private int quanty;
    private String NombreF;
    private Date fecha;

    public Order(Long idPro, String NombreP, int quanty, String NombreF, Date fecha) {
        this.idPro = idPro;
        this.NombreP = NombreP;
        this.quanty = quanty;
        this.NombreF = NombreF;
        this.fecha = fecha;
    }

    /**
     * @return the idPro
     */
    public Long getIdPro() {
        return idPro;
    }

    /**
     * @param idPro the idPro to set
     */
    public void setIdPro(Long idPro) {
        this.idPro = idPro;
    }

    /**
     * @return the NombreP
     */
    public String getNombreP() {
        return NombreP;
    }

    /**
     * @param NombreP the NombreP to set
     */
    public void setNombreP(String NombreP) {
        this.NombreP = NombreP;
    }

    /**
     * @return the NombreF
     */
    public String getNombreF() {
        return NombreF;
    }

    /**
     * @param NombreF the NombreF to set
     */
    public void setNombreF(String NombreF) {
        this.NombreF = NombreF;
    }

    /**
     * @return the fecha
     */
    public Date getFecha() {
        return fecha;
    }

    /**
     * @param fecha the fecha to set
     */
    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }

    /**
     * @return the quanty
     */
    public int getQuanty() {
        return quanty;
    }

    /**
     * @param quanty the quanty to set
     */
    public void setQuanty(int quanty) {
        this.quanty = quanty;
    }
    
}

class OrderTableModel extends AbstractTableModel{
    private List<Order> orders= new ArrayList<Order>();
    private String[] labels={"Id","Nombre","Cantidad", "Proovedor","Fecha"};
    
    int i=0;
    
    public void add(Order or){
        getOrders().add(or);
    }
    
    public void delete(int row){
        getOrders().remove(row);
    }
    
    @Override
    public int getRowCount() {
       return getOrders().size();
    }
    

    @Override
    public String getColumnName(int column) {
        return labels[column];
    }

    @Override
    public int getColumnCount() {
       return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Order or= getOrders().get(rowIndex);
        switch (columnIndex){
            case 0:
            return or.getIdPro();
            case 1:
            return or.getNombreP();
            case 2:
            return or.getQuanty();
            case 3:
            return or.getNombreF();
            case 4:
            return or.getFecha();
            }
        return null;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    /**
     * @return the products
     */
    public List<Order> getOrders() {
        return orders;
    }
    
    


}